//package com.tianji.remark.service.impl;
//
//import com.tianji.api.dto.msg.LikedTimesDTO;
//import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
//import com.tianji.common.constants.MqConstants;
//import com.tianji.common.utils.CollUtils;
//import com.tianji.common.utils.StringUtils;
//import com.tianji.common.utils.UserContext;
//import com.tianji.remark.domain.dto.LikeRecordFormDTO;
//import com.tianji.remark.domain.po.LikedRecord;
//import com.tianji.remark.mapper.LikedRecordMapper;
//import com.tianji.remark.service.ILikedRecordService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 点赞记录表 服务实现类
// * </p>
// *
// * @author author
// * @since 2023-11-10
// */
//@Slf4j
//@Service
//@RequiredArgsConstructor
//public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
//
//    private final RabbitMqHelper rabbitMqHelper;
//    @Override
//    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
//        //1.获取当前登录用户
//        Long userId = UserContext.getUser();
//        //2.判断是否点赞 dto.liked 为true 则是点赞
//        boolean flag = recordDTO.getLiked() ? liked(recordDTO,userId) : unliked(recordDTO,userId);
//        if (!flag){
//            return;
//        }
//
//        //3.统计该业务Id的总点赞数
//        Integer totalLikesNum = this.lambdaQuery()
//                .eq(LikedRecord::getBizId, recordDTO.getBizId())
//                .count();
//        //4.发送消息到mq
//        String routingKey = StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, recordDTO.getBizType());
//        LikedTimesDTO msg = LikedTimesDTO.of(recordDTO.getBizId(), totalLikesNum); //消息体
//        log.debug("发送点赞消息 消息内容：{}",msg);
//        rabbitMqHelper.send(
//                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
//                routingKey,
//                msg);
//    }
//
//    @Override
//    public Set<Long> isBizLiked(List<Long> bizIds) {
//        if (bizIds == null || bizIds.size() == 0){
//            return CollUtils.emptySet();
//        }
//        //1.获取当前登录用户
//        Long userId = UserContext.getUser();
//        //2.查询点赞记录表
//        List<LikedRecord> list = lambdaQuery()
//                .in(LikedRecord::getBizId, bizIds)
//                .eq(LikedRecord::getUserId, userId)
//                .list();
//        //3.查寻到的 bizid转集合
//        Set<Long> likedBizIds = list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
//        return likedBizIds;
//    }
//
//    //取消点赞
//    private boolean unliked(LikeRecordFormDTO recordDTO, Long userId) {
//        //1.判断是否已经点赞
//        LikedRecord record = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, userId)
//                .eq(LikedRecord::getBizId, recordDTO.getBizId())
//                .one();
//        if (record == null){
//            //未点赞
//            return false;
//        }
//        //删除点赞记录
//        boolean result = this.removeById(record.getId());
//        return result;
//    }
//
//    //点赞
//    private boolean liked(LikeRecordFormDTO recordDTO, Long userId) {
//        //1.判断是否已经点赞
//        LikedRecord record = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, userId)
//                .eq(LikedRecord::getBizId, recordDTO.getBizId())
//                .one();
//        if (record != null){
//            //已经点赞
//            return false;
//        }
//        //保存点赞记录
//        LikedRecord likedRecord = new LikedRecord();
//        likedRecord.setUserId(userId);
//        likedRecord.setBizId(recordDTO.getBizId());
//        likedRecord.setBizType(recordDTO.getBizType());
//
//        boolean result = this.save(likedRecord);
//        return result;
//    }
//}
